<?php

namespace App\Console\Commands\OpenShop\CloseShop;

use App\Models\OpenShop\InvestmentPromotion\OpenShopSignOrderModel;
use App\Service\OpenShop\OrderService;
use Illuminate\Console\Command;
use Illuminate\Support\Collection;

class FixCloseShopTypeCommand extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'openShop:fixCloseShopType';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = '门店闭店解约类型数据修复';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return int
     */
    public function handle(): int
    {
        $fix = 0;
        OpenShopSignOrderModel::query()
                              ->where("dissolveStatus", ">", 0)
                              ->chunkById(100, function (Collection $collection) use (&$fix) {
                                  $collection->each(function (OpenShopSignOrderModel $signOrderModel) use (&$fix) {
                                      try {
                                          $invalidType = OrderService::getInvalidOrderType($signOrderModel);
                                          switch ($invalidType) {
                                              case OpenShopSignOrderModel::INVALID_TYPE_1:
                                              case OpenShopSignOrderModel::INVALID_TYPE_2:
                                              case OpenShopSignOrderModel::INVALID_TYPE_3:
                                                  $writeType = OpenShopSignOrderModel::DISSOLVE_STATUS_CANCEL;
                                                  break;
                                              case OpenShopSignOrderModel::INVALID_TYPE_4:
                                                  $writeType = OpenShopSignOrderModel::DISSOLVE_STATUS_CLOSE;
                                                  break;
                                              default:
                                                  throw new \Exception("{$signOrderModel->orderNo} 处理异常");
                                          }
                                          if ($signOrderModel->dissolveStatus != $writeType) {
                                              $this->info("{$signOrderModel->orderNo} {$signOrderModel->dissolveStatus} -> {$writeType}");
                                              $signOrderModel->dissolveStatus = $writeType;
                                              $signOrderModel->save();
                                              $fix++;
                                          }
                                      } catch (\Exception $exception) {
                                          $this->error($exception->getMessage());
                                      }
                                  });
                              });

        $this->info("共计成功更新 {$fix} 条");
        return 0;
    }
}
